package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.BusinesssellerAuthenticationService;
import com.sl.au.service.OptionService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

import com.sl.au.entity.Option;
import com.sl.au.entity.User;

@RestController
@RequestMapping(value = "/ui/bussinessEdit")
public class BusinesssellerEditController extends BaseController{

	@Autowired
	private BusinesssellerBaseInfoService businesssellerBaseInfoService;
    
	@Autowired
	private BusinesssellerAuthenticationService businesssellerAuthenticationService;
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	BussinessLevelService bussinessLevelService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView,HttpServletRequest request) {
		
		String device=request.getParameter("device");
		String identify =request.getParameter("identify");
		if(StringUtils.hasText(device) && device.equals("mobile"))
		{
			modelAndView.setViewName("mobile/businessseller/businessselleredit");
			modelAndView.addObject("identify", identify);
			List<Option> areas =optionService.getRepository().findByGroup("区域范围");
			List<User> areaMpersons =userService.getRepository().findAll();
			List<BussinessLevel> bussinessLevels = bussinessLevelService.getRepository().findAll();
			modelAndView.addObject("areas", areas);
			modelAndView.addObject("bussinessLevels", bussinessLevels);
			modelAndView.addObject("areaMpersons", areaMpersons);
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "getEditDetail/{identify}", method = RequestMethod.GET)
	public BusinesssellerAuthentication getPage(@PathVariable String identify) {
		BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId()); 
		BusinesssellerAuthentication ba =businesssellerAuthenticationService.getRepository().getInfoByBaseInfoId(binfo.getId());
		return ba;
	}
	
	@RequestMapping(value = "saveDetail", method = RequestMethod.POST)
	public Map<String, Object> saveDetail(@RequestBody BusinesssellerAuthentication ba) {
		Map<String, Object> map = new HashMap<String, Object>();
        int code =-1;
        BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId());
        if(binfo.getId().equals(ba.getBusinesssellerBaseInfo().getId())){
		 code =businesssellerAuthenticationService.saveAuthenticationDetail(ba, "初始化");
        }
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "saveDetailWithFile", method = RequestMethod.POST)
	public Map<String, Object> saveDetailWithFile(BusinesssellerAuthentication ba,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
        int code =-1;
        Map<String, String[]> info = request.getParameterMap();
        String levelId = info.get("level")[0];
        BussinessLevel bussinessLevel = bussinessLevelService.getRepository().findOne(levelId);
        BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId());
        binfo.setBussinessLevel(bussinessLevel);
        businesssellerBaseInfoService.getRepository().save(binfo);
        if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
			// 注意路径的构建
			String url = getUploadFilePath(this.fileUploadPath, "img/Businessseller", file);
			ba.setPictureAdress(url);
		}
        if(binfo.getId().equals(ba.getBusinesssellerBaseInfo().getId())){
		 code =businesssellerAuthenticationService.saveAuthenticationDetail(ba, "初始化");
        }
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "submitDetail", method = RequestMethod.POST)
	public Map<String, Object> submitDetail(@RequestBody BusinesssellerAuthentication ba) {
		Map<String, Object> map = new HashMap<String, Object>();
		int code =-1;
        BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId());
        if(binfo.getId().equals(ba.getBusinesssellerBaseInfo().getId())){
	    code =businesssellerAuthenticationService.saveAuthenticationDetail(ba, "待认证");
        }
		map.put("code", code);
		return map;
	}
	
	@RequestMapping(value = "submitDetailWithFile", method = RequestMethod.POST)
	public Map<String, Object> submitDetailWithFile(BusinesssellerAuthentication ba,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		Map<String, Object> map = new HashMap<String, Object>();
		int code =-1;
		Map<String, String[]> info = request.getParameterMap();
        String levelId = info.get("level")[0];
        BussinessLevel bussinessLevel = bussinessLevelService.getRepository().findOne(levelId);
        BusinesssellerBaseInfo binfo= this.businesssellerBaseInfoService.getRepository().getInfoByUserId(this.getMyAccount().getUser().getId());
        binfo.setBussinessLevel(bussinessLevel);
        businesssellerBaseInfoService.getRepository().save(binfo);
        if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
			// 注意路径的构建
			String url = getUploadFilePath(this.fileUploadPath, "img/Businessseller", file);
			ba.setPictureAdress(url);
		}
        if(binfo.getId().equals(ba.getBusinesssellerBaseInfo().getId())){
	    code =businesssellerAuthenticationService.saveAuthenticationDetail(ba, "待认证");
        }
		map.put("code", code);
		return map;
	}
}
